<?php
require("top.ajax.php");

if(isset($_POST['action']) && $_POST['action']=='addcomment'){
    $page=1;
    $event = new Event($_POST['id']);
    $commentaire = nl2br(addslashes(htmlentities(utf8_decode($_POST['comment']))));
    $id_user=$_POST['id_user'];
    $comment = $event->addComment($commentaire,$id_user);

    $tpl->assign('comment',$comment);
    $tpl->display("ajax/comment.tpl");

}else if(isset($_POST['action']) && $_POST['action']=='delcomment'){
    $event = new Event($_POST['id']);
    $eventcomment = new EventComment($_POST['id_commentaire']);
    $eventcomment->delete();
    $nb_comments_by_page = 5;
    $comments = $event->getComments();

    $page = 1;
    $pager = array();
    $pager['nb_total'] = ceil(count($comments) / $nb_comments_by_page);
    $pager['nb_page_begin'] = ($pager['nb_total']<5)?$pager['nb_total']:5;
    $pager['begin'] = ($page > 3 && $pager['nb_total'] > 5) ? ($page+2 > $pager['nb_total']) ? $pager['nb_total']-4 : $page - 2 : 1;
    $pager['current'] = $page;
    $pager['end'] = ($page+2 > $pager['nb_total']) ? $pager['nb_total']+1 : $pager['begin']+$pager['nb_page_begin'];
    $pager['link'] = '';

    $indice['min']=($page-1)*$nb_comments_by_page+1;
    $indice['max']= $nb_comments_by_page;

    $tpl->assign('comments',$comments);
    $tpl->assign('event', $event);
    $tpl->assign('pager', $pager);
    $tpl->assign('indice', $indice);
    $tpl->display("ajax/eventcomment.tpl");

}else if(isset($_POST['action']) && $_POST['action']=='pager'){
    $nb_comments_by_page = 5;
    $event = new Event($_POST['id']);
    $comments = $event->getComments();

    $page = $_POST['page'];
    $pager = array();
    $pager['nb_total'] = ceil(count($comments) / $nb_comments_by_page);
    $pager['nb_page_begin'] = ($pager['nb_total']<5)?$pager['nb_total']:5;
    $pager['begin'] = ($page > 3 && $pager['nb_total'] > 5) ? ($page+2 > $pager['nb_total']) ? $pager['nb_total']-4 : $page - 2 : 1;
    $pager['current'] = $page;
    $pager['end'] = ($page+2 > $pager['nb_total']) ? $pager['nb_total']+1 : $pager['begin']+$pager['nb_page_begin'];
    $pager['link'] = '';

    $indice['min']=($page-1)*$nb_comments_by_page+1;
    $indice['max']= $nb_comments_by_page;

    $tpl->assign('comments',$comments);
    $tpl->assign('event', $event);
    $tpl->assign('pager', $pager);
    $tpl->assign('indice', $indice);
    $tpl->display("ajax/eventcomment.tpl");
}
?>